package com.zhuss.food.food;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.food.food.DetailFoodBo;
import com.zhuss.bean.bo.food.food.ListFoodByCategoryBo;
import com.zhuss.bean.bo.food.food.SearchFoodBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.food.food.DetailFoodVo;
import com.zhuss.bean.vo.food.food.ListFoodVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.core.processor.EnumConfig;
import com.zhuss.exception.WpsException;
import com.zhuss.food.processor.FoodRead;


/**
 * 餐品查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020/02/28 16:01
 *
 */
@Component("foodReadService")
public class FoodReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("foodRead")
	private FoodRead foodRead;
	
	@Autowired
	@Qualifier("enumConfig")
	private EnumConfig enumConfig;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailFoodBo bo = (DetailFoodBo) param;
		Long foodId = bo.getFoodId();
		DetailFoodVo vo = (DetailFoodVo) foodRead.detail(foodId);
		vo.setCategoryName(enumConfig.categoryName(vo.getCategory(), vo.getArea()));
		String pic = vo.getPic();
		String picUrl = null;
		if (pic == null || "".equals(pic.trim())) {
			picUrl = null;
		} else {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			picUrl = downloadVo.getUrl();
		}
		vo.setPicUrl(picUrl);
		return vo;
	}
	
	/**
	 * 列表
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Object list(Object param) {
		SearchFoodBo bo = (SearchFoodBo) param;
		Object result = foodRead.list(bo);
		List<ListFoodVo> list = ((PageVo<ListFoodVo>) result).getItems();
		for (ListFoodVo item : list) {
			String categoryName = enumConfig.categoryName(item.getCategory(), item.getArea());
			item.setCategoryName(categoryName);
		}
		return result;
	}
	
	public Object listByCategory(ListFoodByCategoryBo bo) {
		Object result = foodRead.listByCategorySelect(bo.getCategory(), bo.getArea());
		return result;
	}
	
	
	
}

